const jwt = require('jsonwebtoken');
const secret = 'expresstest-anydata';
const JWT = {
    generateToken(data,expires){
        return jwt.sign(data,secret,{expiresIn:expires})
    },
    verifyToken(token,res,next){
        return jwt.verify(token,secret,(err,decoded)=>{
            if(err){
                switch (err.name) {
                    case 'TokenExpiredError':
                        res.status(403).send({
                            code: 0,
                            message: 'token过期'
                        });
                        break;
                    case 'JsonWebTokenError':
                        res.status(403).send({
                            code: 0,
                            message: '无效的token'
                        });
                        break;
                }
            }else{
                next()
            }
        })
    }
}

module.exports = JWT

